<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="110px"
    >
      <el-form-item label="小区名称" prop="deptId">
        <!-- <el-input
          v-model="queryParams.deptId"
          placeholder="请输入小区名称"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-cascader
          v-model="queryParams.deptId"
          :options="deptOptions"
          @change="handleChange"
          placeholder="请选择所属部门"
        >
        </el-cascader>
      </el-form-item>
      <el-form-item label="住户身份证号" prop="residentId">
        <el-input
          v-model="queryParams.residentId"
          placeholder="请输入住户身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <!-- <el-form-item label="营销人员" prop="marketingUserId">
        <el-input
          v-model="queryParams.marketingUserId"
          placeholder="请输入营销人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="套餐类型">
        <el-select
          v-model="queryParams.packageType"
          placeholder="请选择套餐类型："
          clearable
        >
          <el-option
            v-for="dict in dict.type.package_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="套餐名称" prop="packageName">
        <el-input
          v-model="queryParams.packageName"
          placeholder="请输入套餐名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="套餐价格" prop="packagePrice">
        <el-input
          v-model="queryParams.packagePrice"
          placeholder="请输入套餐价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="业务类型" prop="businessType">
        <el-select
          v-model="queryParams.businessType"
          placeholder="请选择业务类型："
          clearable
        >
          <el-option
            v-for="dict in dict.type.business_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="办理时间" prop="businessDate">
        <el-date-picker
          clearable
          v-model="queryParams.businessDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择办理时间"
        >
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="预期收入" prop="expectedIncome">
        <el-input
          v-model="queryParams.expectedIncome"
          placeholder="请输入预期收入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实际收入" prop="actualIncome">
        <el-input
          v-model="queryParams.actualIncome"
          placeholder="请输入实际收入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否草稿" prop="isDraft">
        <el-input
          v-model="queryParams.isDraft"
          placeholder="请输入是否草稿"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="审核状态：" prop="auditStatus">
        <el-select
          v-model="queryParams.auditStatus"
          placeholder="请选择审核状态："
          clearable
        >
          <el-option
            v-for="dict in dict.type.audit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核人" prop="auditUserId">
        <el-input
          v-model="queryParams.auditUserId"
          placeholder="请输入审核人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核时间" prop="auditTime">
        <el-date-picker
          clearable
          v-model="queryParams.auditTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择审核时间"
        >
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="是否计入业绩：" prop="performanceIncluded">
        <el-input
          v-model="queryParams.performanceIncluded"
          placeholder="请输入是否计入业绩："
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="生效日期" prop="effectiveDate">
        <el-date-picker
          clearable
          v-model="queryParams.effectiveDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择生效日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="到期日期" prop="expireDate">
        <el-date-picker
          clearable
          v-model="queryParams.expireDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择到期日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态：" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态："
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_oper_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:business:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:business:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:business:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:business:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="businessList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="业务ID"
        align="center"
        prop="id"
        v-if="columns[0].visible"
      />
      <el-table-column
        label="小区名称"
        align="center"
        prop="deptId"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          {{ getDeptName(scope.row.deptId) }}
        </template>
      </el-table-column>
      <el-table-column
        label="身份证号"
        align="center"
        prop="residentId"
        :show-overflow-tooltip="true"
      />

      <!-- <el-table-column label="营销人员" align="center" prop="nickName">
        <template slot-scope="scope">
          <span>{{ scope.row.marketingUserId == userId ? userName + '(' + nickName + ')' : scope.row.marketingUserId }}</span> 
          <span>{{ scope.row.marketingUserId == userId ?nickName : scope.row.marketingUserId }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="套餐类型" align="center" prop="packageType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.package_type"
            :value="scope.row.packageType"
          />
        </template>
      </el-table-column>
      <el-table-column label="套餐名称" align="center" prop="packageName" />
      <!-- <el-table-column label="套餐价格" align="center" prop="packagePrice" /> -->
      <el-table-column label="业务类型" align="center" prop="businessType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.business_type"
            :value="scope.row.businessType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="办理时间"
        align="center"
        prop="businessDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.businessDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="预期收入" align="center" prop="expectedIncome" />
      <el-table-column label="实际收入" align="center" prop="actualIncome" /> -->
      <el-table-column label="是否草稿" align="center" prop="isDraft">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yn" :value="scope.row.isDraft" />
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.audit_status"
            :value="scope.row.auditStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditUserId" />
      <el-table-column
        label="审核时间"
        align="center"
        prop="auditTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核备注" align="center" prop="auditRemark" />
      <el-table-column
        label="是否计入业绩"
        align="center"
        prop="performanceIncluded"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_yn"
            :value="scope.row.performanceIncluded"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="生效日期"
        align="center"
        prop="effectiveDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.effectiveDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="到期日期"
        align="center"
        prop="expireDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expireDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="相关照片" align="center" prop="photos" /> -->
      <!-- <el-table-column label="备注" align="center" prop="remarks" /> -->
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_oper_type"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="150"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:business:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:business:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['system:business:query']"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改营销业务办理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="小区名称" prop="deptId">
              <!-- <el-input v-model="form.deptId" placeholder="请输入小区名称" /> -->
              <!-- <el-cascader
                v-model="form.deptId"
                :options="deptOptions"
                @change="handleChange"
                placeholder="请选择所属部门"
              >
              </el-cascader> -->
              <el-cascader
                v-model="form.deptId"
                :options="deptOptions"
                @change="handleChange"
                placeholder="请选择所属部门"
              >
              </el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="住户身份证号" prop="residentId">
              <el-input
                v-model="form.residentId"
                placeholder="请输入住户身份证号"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="营销人员" prop="marketingUserId">
              <el-input readonly v-model="nickName" placeholder="营销人员" />
              <input type="hidden" v-model="form.marketingUserId" value="1" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="套餐类型" prop="packageType">
              <el-select
                v-model="form.packageType"
                placeholder="请选择套餐类型"
              >
                <el-option
                  v-for="dict in dict.type.package_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="套餐名称" prop="packageName">
              <el-input
                v-model="form.packageName"
                placeholder="请输入套餐名称"
              /> </el-form-item
          ></el-col>
          <el-col :span="12"
            ><el-form-item label="套餐价格" prop="packagePrice">
              <el-input
                v-model="form.packagePrice"
                placeholder="请输入套餐价格"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="业务类型" prop="businessType">
              <el-select
                v-model="form.businessType"
                placeholder="请选择业务类型"
              >
                <el-option
                  v-for="dict in dict.type.business_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="办理时间" prop="businessDate">
              <el-date-picker
                clearable
                v-model="form.businessDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择办理时间"
              >
              </el-date-picker> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="预期收入" prop="expectedIncome">
              <el-input
                v-model="form.expectedIncome"
                placeholder="请输入预期收入"
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="实际收入" prop="actualIncome">
              <el-input
                v-model="form.actualIncome"
                placeholder="请输入实际收入"
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="是否草稿" prop="isDraft">
              <!-- <el-input v-model="form.isDraft" placeholder="请输入是否草稿" /> -->
              <el-radio-group v-model="form.isDraft">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="2">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24"
            ><el-form-item label="审核状态" prop="auditStatus">
              <el-radio-group v-model="form.auditStatus">
                <el-radio
                  v-for="dict in dict.type.audit_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12"
            ><el-form-item label="审核人" prop="auditUserId">
              <el-input v-model="form.auditUserId" placeholder="请输入审核人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核时间" prop="auditTime">
              <el-date-picker
                clearable
                v-model="form.auditTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择审核时间"
              >
              </el-date-picker> </el-form-item
          ></el-col>
          <el-col :span="24">
            <el-form-item label="审核备注" prop="auditRemark">
              <el-input
                v-model="form.auditRemark"
                type="textarea"
                placeholder="请输入内容"
              /> </el-form-item
          ></el-col>
          <el-col :span="24">
            <el-form-item label="是否计入业绩：" prop="performanceIncluded">
              <!-- <el-input
                v-model="form.performanceIncluded"
                placeholder="请输入是否计入业绩"
              />  -->
              <el-radio-group v-model="form.performanceIncluded">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="2">否</el-radio>
              </el-radio-group>
            </el-form-item></el-col
          >
          <el-col :span="12">
            <el-form-item label="生效日期" prop="effectiveDate">
              <el-date-picker
                clearable
                v-model="form.effectiveDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择生效日期"
              >
              </el-date-picker> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="到期日期" prop="expireDate">
              <el-date-picker
                clearable
                v-model="form.expireDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择到期日期"
              >
              </el-date-picker> </el-form-item
          ></el-col>
          <el-col :span="24">
            <el-form-item label="相关照片" prop="photos">
              <!-- <el-input
                v-model="form.photos"
                type="textarea"
                placeholder="请输入内容"
              /> -->
              <imageUpload
                v-model="form.photos"
                :imgUrl="form.photos"
                :limit="4"
                text-center
              >
              </imageUpload>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remarks">
              <el-input
                v-model="form.remarks"
                type="textarea"
                placeholder="请输入内容"
              /> </el-form-item
          ></el-col>
          <el-col :span="24">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sys_oper_type"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item></el-col
          >
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 营销业务办理信息详情 -->
    <el-dialog
      title="营销业务办理信息详情"
      :visible.sync="openDetail"
      width="800px"
      append-to-body
    >
      <el-form ref="form" :model="form" label-width="120px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="小区名称："
              ><template slot-scope="">
                {{ getDeptName(form.deptId) }}
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="住户身份证号：">{{ form.residentId }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="营销人员：">
              {{nickName}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="套餐类型：">
              <dict-tag
                :options="dict.type.package_type"
                :value="form.packageType"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="套餐名称：">{{ form.packageName }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="套餐价格：">
              {{ form.packagePrice }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="业务类型：">
              <dict-tag
                :options="dict.type.business_type"
                :value="form.businessType"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12"> 
            <el-form-item label="办理时间：">
              {{ form.businessDate }}
            </el-form-item>
          </el-col>
          <el-col :span="12"> 
            <el-form-item label="预期收入：">
              {{ form.expectedIncome }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实际收入：">
              {{ form.actualIncome }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否草稿：">
              <dict-tag :options="dict.type.sys_yn" :value="form.isDraft" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核状态：">
              <dict-tag
                :options="dict.type.audit_status"
                :value="form.auditStatus"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核人：">{{ form.auditUserId }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核时间：">{{ form.auditTime }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核备注：">{{ form.auditRemark }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否计入业绩：">
              <dict-tag
                :options="dict.type.sys_yn"
                :value="form.performanceIncluded"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生效日期：">{{ form.effectiveDate }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="到期日期：">{{ form.expireDate }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="相关照片：">
              <div v-if="form.photos">
                <!-- <img :src="form.photos" alt="" width="100px" height="100px" /> -->
                <ImagePreview :src="form.photos" width="100px" height="100px" />
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注：">{{ form.remarks }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态：">
              <dict-tag
                :options="dict.type.sys_oper_type"
                :value="form.status"
              />
            </el-form-item>
          </el-col> 
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openDetail = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBusiness,
  getBusiness,
  delBusiness,
  addBusiness,
  updateBusiness,
} from "@/api/system/business";
import { getDeptTree } from "@/api/system/user";
import { listDept } from "@/api/system/dept";
import store from "@/store";
export default {
  name: "Business",
  dicts: [
    "sys_oper_type",
    "audit_status",
    "package_type",
    "business_type",
    "sys_yn",
  ],
  data() {
    return {
      radio: "1",
      userId: store.state.user.id, //将缓存的用户id赋值给userId，用于查询营销人员
      userName: store.state.user.name, //将缓存的用户id赋值给userId，用于查询营销人员
      nickName: store.state.user.nickName, //将缓存的用户id赋值给userId，用于查询营销人员
      deptId: store.state.user.deptId, //将缓存的用户id赋值给userId，用于查询营销人员
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 营销业务办理表格数据
      businessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDetail: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        residentId: null,
        deptId: null,
        marketingUserId: null,
        packageType: null,
        packageName: null,
        packagePrice: null,
        businessType: null,
        businessDate: null,
        expectedIncome: null,
        actualIncome: null,
        isDraft: null,
        auditStatus: null,
        auditUserId: null,
        auditTime: null,
        auditRemark: null,
        performanceIncluded: null,
        effectiveDate: null,
        expireDate: null,
        photos: null,
        remarks: null,
        status: null,
      },
      deptOptions: [],
      // 列信息
      columns: [{ key: 0, label: `业务ID`, visible: false }],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        residentId: [
          { required: true, message: "住户ID不能为空", trigger: "blur" },
        ],
        deptId: [
          { required: true, message: "所属部门ID不能为空", trigger: "blur" },
        ],
        // marketingUserId: [
        //   { required: true, message: "营销人员不能为空", trigger: "blur" },
        // ],
        packagePrice: [
          { required: true, message: "套餐价格不能为空", trigger: "blur" },
        ],
        businessType: [
          {
            required: true,
            message: "业务类型不能为空",
            trigger: "change",
          },
        ],
        businessDate: [
          { required: true, message: "办理时间不能为空", trigger: "blur" },
        ],
        expectedIncome: [
          { required: true, message: "预期收入不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getDeptList();
    this.getDeptTree(100);
    this.getList();
  },
  methods: {
    /** 查询部门下拉树结构 */
    async getDeptTree(deptId) {
      getDeptTree({ deptId: deptId }).then((response) => {
        if (deptId == 100) {
          var treeData = response.data.children;
          treeData.map((item) => {
            item.label = item.deptName;
            item.value = item.deptId;
            if (item.children && item.children.length) {
              item.children.forEach((child) => {
                child.label = child.deptName;
                child.value = child.deptId;
                if (child.children && child.children.length) {
                  child.children.forEach((grandson) => {
                    grandson.label = grandson.deptName;
                    grandson.value = grandson.deptId;
                    delete grandson.children;
                  });
                }
              });
            }
          });
          this.deptOptions = treeData;
        }
      });
    },
    handleChange(value) {
      console.log(value, "value");
      console.log(value[1], "value");
      //this.form.deptId = value[1];
    },
    getDeptList() {
      listDept().then((response) => {
        this.deptList = response.data;
        this.deptList = this.filterDisabledDept(this.deptList);
      });
    },

    // 过滤禁用的部门
    filterDisabledDept(deptList) {
      return deptList.filter((dept) => {
        if (dept.disabled) {
          return false;
        }
        if (dept.children && dept.children.length) {
          dept.children = this.filterDisabledDept(dept.children);
        }
        return true;
      });
    },
    getDeptName(deptId) {
      return this.deptList.find((item) => item.deptId == deptId)?.deptName;
    },
    /** 查询营销业务办理列表 */
    getList() {
      this.loading = true;
      listBusiness(this.queryParams).then((response) => {
        this.businessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        residentId: null,
        deptId: null,
        marketingUserId: store.state.user.id,
        packageType: null,
        packageName: null,
        packagePrice: null,
        businessType: null,
        businessDate: null,
        expectedIncome: null,
        actualIncome: null,
        isDraft: 1,
        auditStatus: null,
        auditUserId: null,
        auditTime: null,
        auditRemark: null,
        performanceIncluded: null,
        effectiveDate: null,
        expireDate: null,
        photos: null,
        remarks: null,
        status: null,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加营销业务办理";
      this.deptOptions = [];
      this.getDeptTree(100);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBusiness(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改营销业务办理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBusiness(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBusiness(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.openDetail = true;
      this.form = row;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除营销业务办理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBusiness(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/business/export",
        {
          ...this.queryParams,
        },
        `business_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
